package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.Vehicle;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.service.IVehicleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 车辆信息Controller
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/vehicle")
public class VehicleController extends BaseController {
    @Autowired
    private IVehicleService vehicleService;

    /**
     * 获取车辆列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Vehicle vehicle) {
        startPage();
        List<Vehicle> list = vehicleService.selectVehicleByPhone(vehicle);
        return getDataTable(list);
    }

    @Log(title = "车辆管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Vehicle vehicle) {
        List<Vehicle> list = vehicleService.selectVehicleList(vehicle);
        ExcelUtil<Vehicle> util = new ExcelUtil<Vehicle>(Vehicle.class);
        util.exportExcel(response, list, "车辆信息数据");
    }

    /**
     * 根据车辆编号获取详细信息
     */
    @GetMapping(value = { "/", "/{vehicleId}" })
    public AjaxResult getInfo(@PathVariable(value = "vehicleId", required = false) Long vehicleId) {
        AjaxResult ajax = AjaxResult.success();
        if (StringUtils.isNotNull(vehicleId)) {
            Vehicle vehicle = vehicleService.selectVehicleById(vehicleId);
            ajax.put(AjaxResult.DATA_TAG, vehicle);
        }
        return ajax;
    }

    /**
     * 新增车辆
     */
    @Log(title = "车辆管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody Vehicle vehicle) {

        if (!vehicleService.checkLicensePlateUnique(vehicle)) {
            // 车牌号已存在，返回 code 为 4001 的提示信息
            return AjaxResult.error(4001, "该车牌已被绑定");
        }

        vehicle.setCreateBy(SecurityUtils.getUsername());
        vehicleService.insertVehicle(vehicle);
        return AjaxResult.success("添加成功");
    }

    /**
     * 修改车辆
     */
    @Log(title = "车辆管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Vehicle vehicle) {
        // 检查车辆是否存在
        if (vehicleService.selectVehicleById(vehicle.getId()) == null) {
            return error("修改车辆失败，车辆不存在");
        }

        // 检查车牌号是否唯一（排除自身）
        if (!vehicleService.checkLicensePlateUnique(vehicle)) {
            return error("修改车辆'" + vehicle.getLicensePlate() + "'失败，车牌号已存在");
        }

        vehicle.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(vehicleService.updateVehicle(vehicle));
    }

    /**
     * 删除车辆
     */
    @Log(title = "车辆管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{vehicleIds}")
    public AjaxResult remove(@PathVariable Long[] vehicleIds) {
        return toAjax(vehicleService.deleteVehicleByIds(vehicleIds));
    }

    /**
     * 逐个删除车辆
     */
    @Log(title = "车辆管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/deleteOne/{vehicleId}")
    public AjaxResult removeOne(@PathVariable Long vehicleId) {
        return toAjax(vehicleService.deleteVehicleById(vehicleId));
    }



}
